package com.zf.zffencheng.util;



import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.stream.Collectors;

public class DateUtil {
    private static final String NOW_DATE="yyyy-MM-dd";
    private static final String NOW_TIME="yyyy-MM-dd HH:mm:ss";

    /**
     * 得到当前日期 yyyy-MM-dd
     */
    public static String getNowDate() {
        return LocalDate.now().toString();
    }
    /**
     * 得到传输日期字符串yyyy-MM-dd
     * @param date
     */
    public static String getNowDate(Date date){
        return format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()), NOW_DATE);
    }

    /**
     * 得到传输时间字符串yyyy-MM-dd HH:mm:ss
     * @param date
     */
    public static String getNowTime(Date date){
        return format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()), NOW_TIME);
    }

    /**
     * localDateTime转换为格式化时间
     * @param localDateTime
     * @param pattern 格式
     */
    public static String format(LocalDateTime localDateTime, String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }
    /**
     * 计算日期间隔月数
     *
     * @param start
     * @param end
     * @return
     */
    public static int monthsBetween(Date start, Date end) {
        Months months = Months.monthsBetween(new DateTime(start), new DateTime(end));

        return months.getMonths();
    }

    /**
     * 计算日期间隔天数
     *
     * @param start
     * @param end
     * @return
     */
    public static int daysBetween(Date start, Date end) {
        Days days = Days.daysBetween(new DateTime(start), new DateTime(end));

        return days.getDays();
    }

    /**
     * 计算日期间隔分钟数
     *
     * @param start
     * @param end
     * @return
     */
    public static int minutesBetween(Date start, Date end) {
        Minutes minutes = Minutes.minutesBetween(new DateTime(start), new DateTime(end));

        return minutes.getMinutes();
    }

    /**
     * 计算日期间隔秒数
     *
     * @param start
     * @param end
     * @return
     */
    public static int secondsBetween(Date start, Date end) {
        Seconds seconds = Seconds.secondsBetween(new DateTime(start), new DateTime(end));
        return seconds.getSeconds();
    }

    /**
     * 加减多少年 amount为负数就是减去多少年
     * @param date
     * @param amount
     * @return
     */
    public static Date addYears(Date date, int amount) {
        return DateUtils.addYears(date, amount);
    }

    /**
     * 加减多少月 amount为负数就是减去多少月
     * @param date
     * @param amount
     * @return
     */
    public static Date addMonths(Date date, int amount) {
        return DateUtils.addMonths(date, amount);
    }

    /**
     * 加减多少天 amount为负数就是减去多少天
     * @param date
     * @param amount
     * @return
     */
    public static Date addDays(Date date, int amount) {
        return DateUtils.addDays(date, amount);
    }


    /**
     * 加减多少小时 amount为负数就是减去多少小时
     * @param date
     * @param amount
     * @return
     */
    public static Date addHours(Date date, int amount) {
        return DateUtils.addHours(date, amount);
    }

    /**
     * 加减多少分钟 amount为负数就是减去多少分钟
     * @param date
     * @param amount
     * @return
     */
    public static Date addMinutes(Date date, int amount) {
        return new DateTime(date).plusMinutes(amount).toDate();
    }

    /**
     * 加减多少秒 amount为负数就是减去多少秒
     * @param date
     * @param amount
     * @return
     */
    public static Date addSeconds(Date date, int amount) {
        return new DateTime(date).plusSeconds(amount).toDate();
    }

    /**
     * 获取本周的星期一时几号
     * @param date
     * @return
     */
    public static Date getWeek(Date date) {
        int mondayPlus;
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            mondayPlus = 0;
        } else {
            mondayPlus = 1 - dayOfWeek;
        }
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        currentDate.set(GregorianCalendar.HOUR_OF_DAY, 0);
        currentDate.set(GregorianCalendar.MINUTE, 0);
        currentDate.set(GregorianCalendar.SECOND, 0);
        currentDate.set(GregorianCalendar.MILLISECOND, 0);
        return currentDate.getTime();
    }

    /**
     * 当前时间转换为秒
     * @param date
     * @return
     */
    public static long date2Timestamp(Date date) {
        return date.getTime() / 1000;
    }

    /**
     * 秒转换时间
     * @param timestamp
     * @return
     */
    public static Date timestamp2Date(long timestamp) {
        return new Date(timestamp * 1000);
    }

    public static void main(String[] args) {
        System.out.println(getWeek(null));
    }

    public static Date getAfterDay(Date date, Integer amount) {
        return new Date(DateUtils.addDays(date, amount).getTime());
    }

    public static List<Integer> removeDuplicateId(List<Integer> list) {
        List<Integer> unique = list.stream().distinct().collect(Collectors.toList());
        return unique;
    }

    /**
     * 找到数组中指定的元素的下标
     *
     * @return
     */
    public static int printArray(String[] arr, String value) {

        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(value)) {
                return i;
            }
        }
        return -1;
    }

    public static String getStringDate(String pattern, Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

}
